import { defineStore } from 'pinia'

const useGlobalStore = defineStore({
  // 这里的id必须为唯一ID
  id: 'useGlobalStore',
  state: (): Common.globalData => {
    return {
      res_success: '000',
      userInfo: null,
      token: 'accc',
      username: '',
      password: '',
      nowRoute: '',
      arrayCacheRoute: [],
      permissions: []
    }
  },
  // 等同于vuex的getter
  getters: {},
  // pinia 放弃了 mutations 只使用 actions
  actions: {
    // actions可以用async做成异步形式
    setUserInfo(userInfo: ApiUser.UserInfo) {
      this.userInfo = userInfo
    },
    setToken(token: string) {
      this.token = token
    },
    setPermissions(permiss: string[]) {
      this.permissions = permiss || []
    },
    setUsername(username: string) {
      this.username = username
    },
    setPassword(password: string) {
      this.password = password
    },
    setArrayCacheRoute(arrayCacheRoute: any) {
      this.arrayCacheRoute = arrayCacheRoute
    },
    setNowRoute(nowRoute: string) {
      this.nowRoute = nowRoute
    },
    setLoginOut() {
      this.userInfo = null
      this.token = ''
      this.username = ''
      this.password = ''
      this.nowRoute = ''
      this.arrayCacheRoute = []
      this.permissions = []
    }
  },
  // 持久化
  persist: {
    key: 'ai-painting-web',
    storage: window.sessionStorage
    // storage: window.localStorage
  }
})

export default useGlobalStore
